pub fn convert_base(num_str: &str, to_base: u32) -> String {
    // 检查目标进制范围
    if to_base < 2 || to_base > 16 {
        return String::from("Target base must be between 2 and 16");
    }

    // Step 1: 解析输入的数值和原始进制（如 "9(10)"）
    let parts: Vec<&str> = num_str.split('(').collect();
    if parts.len() != 2 || !parts[1].ends_with(')') {
        return String::from("Invalid input format"); // 返回错误信息
    }

    // 获取数字字符串和原始进制
    let number = parts[0];
    let from_base = match parts[1][..parts[1].len() - 1].parse::<u32>() {
        Ok(base) => base,
        Err(_) => return String::from("Invalid base"), // 返回错误信息
    };

    // 检查原始进制范围
    if from_base < 2 || from_base > 16 {
        return String::from("Base must be between 2 and 16");
    }

    // Step 2: 将字符串解析为十进制整数
    let decimal_value = match u32::from_str_radix(number, from_base) {
        Ok(value) => value,
        Err(_) => return String::from(format!("Invalid number '{}' for base '{}'", number, from_base)),
    };

    // println!("Converted '{}' from base {} to decimal: {}", number, from_base, decimal_value);

    // Step 3: 将十进制数转换为目标进制
    let result = decimal_to_base(decimal_value, to_base);
    // println!("Converted decimal {} to base {}: {}", decimal_value, to_base, result);
    
    result
}

fn decimal_to_base(mut number: u32, base: u32) -> String {
    let chars = "0123456789abcdef";  // 使用小写字母
    let mut result = String::new();

    if number == 0 {
        return "0".to_string();
    }

    while number > 0 {
        let remainder = (number % base) as usize;
        result.push(chars.chars().nth(remainder).unwrap());
        number /= base;
    }

    result.chars().rev().collect()
}
